'use strict';

import {ajax, nameSpace } from 'utils/index';
import jsonp from 'jsonp';

let ns = nameSpace('RingTagsStep2');

export const SET_FIELD = ns('SET_FIELD');
export const SET_ZHEKOU_TYPE = ns('SET_ZHEKOU_TYPE');
export const CREAT_ZHEKOU_START = ns('CREAT_ZHEKOU_START');
export const CREAT_ZHEKOU_SET_ITEMS = ns('CREAT_ZHEKOU_SET_ITEMS');
export const CREAT_ZHEKOU_NEED_ITEMS = ns('CREAT_ZHEKOU_NEED_ITEMS');
export const CREAT_ZHEKOU_DONE = ns('CREAT_ZHEKOU_DONE');
export const CREAT_ZHEKOU_ERR = ns('CREAT_ZHEKOU_ERR');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const TASK_INFO = ns('NO_TASK_INFO');

export function setField(name,value) {
  return {type: SET_FIELD,name: name,value:value};
}
export function setZhekouType(value) {
  return {type: SET_ZHEKOU_TYPE,value:value};
}
export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function tagTaskWell() {
  return {type: TASK_INFO,error:false,noTask:false,message:"没有标签人群信息，无法创建折扣！"};
}
export function tagTaskError() {
  return {type: TASK_INFO,error:true,noTask:true,message:"没有标签人群信息，无法创建折扣！"};
}
export function stepNeedItems() {
  return {type: CREAT_ZHEKOU_NEED_ITEMS,needItems:false};
}
export function createShopZhekou(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: CREAT_ZHEKOU_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行创建折扣,含有workbench参数为千牛环境
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.item.activity.add',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'createTaskShopActive',
			  method: 'GET',
			  data:params
			},(json) => {
				//在线创建成功之后，保存到数据库
				ajax({
				  api: 'saveTaskActive',
				  method: 'GET',
				  data:{...params,activity_id:json.activity_id}
				},(save_json) => {
					  //全店参与的，进入最后一个环节，暂存活动ID
					  dispatch({
						type: CREAT_ZHEKOU_DONE,
						activityId: json.activity_id,
						actionDone:true
					  });
				},(json) => {
					  //(json.code==53) W2超时
					  const login = (json.code==210);
					  dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
						});
				});

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: CREAT_ZHEKOU_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  }
}
  
export function createItemZhekou(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: CREAT_ZHEKOU_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行创建折扣,含有workbench参数为千牛环境
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.common.item.activity.add',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'createTaskItemActive',
			  method: 'GET',
			  data:params
			},(json) => {
				//在线创建成功之后，保存到数据库
				ajax({
				  api: 'saveTaskActive',
				  method: 'GET',
				  data:{...params,activity_id:json.activity_id}
				},(save_json) => {
					  //部分商品参与，要进入设置商品环节，暂存活动ID
					  dispatch({
						type: CREAT_ZHEKOU_SET_ITEMS,
						activityId: json.activity_id,
						needItems:true,
						actionDone:false
					  });
				},(json) => {
					  //(json.code==53) W2超时
					  const login = (json.code==210);
					  dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
						});
				});

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: CREAT_ZHEKOU_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}
export function createLimitZhekou(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: CREAT_ZHEKOU_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行创建折扣,含有workbench参数为千牛环境
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.common.item.activity.add',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'createTaskLimitActive',
			  method: 'GET',
			  data:params
			},(json) => {
				//在线创建成功之后，保存到数据库
				ajax({
				  api: 'saveTaskActive',
				  method: 'GET',
				  data:{...params,activity_id:json.act_id,act_type:"limit"}
				},(save_json) => {
					  //部分商品参与，要进入设置商品环节，暂存活动ID
					  dispatch({
						type: CREAT_ZHEKOU_SET_ITEMS,
						activityId: json.act_id,
						needItems:true,
						actionDone:false,
						act_type:"limit"
					  });
				},(json) => {
					  //(json.code==53) W2超时
					  const login = (json.code==210);
					  dispatch({
						type: CREAT_ZHEKOU_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
						});
				});

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: CREAT_ZHEKOU_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}


